var epointuser = {
    type: "",
    scale: ""
};
///:用户主页
(function($) {
    var $useInfo = $('#user-info');
    var userInfoTmpl = Util.clearHtml($('#userInfo-tmpl').html());
    var M = Mustache;
    var the_userid = $.cookie('the_userid');
    M.parse(userInfoTmpl);
    // 用户信息
    $.ajax({
        url: Util.AjaxRUrl + 'userInfo',
        type: "post",
        dataType: "json",
        async: false, //用户类型需先确定

        data: {
            param: '{userGuid:"' + the_userid + '"}'
        },
        success: function(data) {
            //console.log(data);
            renderData(data);
        },
        error: function(XMLHttpRequest, txtStatus, errorThrown) {
            //console.log(XMLHttpRequest);
        }
    });
    // var userdata={
    //      UserInfo:{
    //          loginId:$.cookie('userName')
    //      }
    //  }
    //renderData(userdata);
    // 验证状态:是否认证
    $.ajax({
        url: Util.AjaxRUrl + 'certificateStatus',
        type: "post",
        dataType: "json",
        data: {
            param: '{userGuid:"' + the_userid + '"}' //json字符串
        },
        success: function(data) {
            // console.log(msg);
            renderStateData(data);
        },
        error: function(XMLHttpRequest, txtStatus, errorThrown) {
            //console.log(XMLHttpRequest);
        }
    });
    //用户信息渲染
    function renderData(data) {
        if (data) {
            if (data.userInfo) {
                epointuser.type = data.userInfo.registType;
                // 用户配置
                $(".ewb-operation").load("/template/include/userconfig.html", function(rdata) {
                    $(".ewb-operation").html(rdata);
                    //2017-04-06wurf,厂房出承租用
                    epointuser.unitName=data.userInfo.unitName;
                    epointuser.tyshxyCode=data.userInfo.tyshxyCode;
                    if (epointuser.type == "企业注册"){
                        $(".renterprise").show();
                        if(data.userInfo.qyType=="1"){
                            $(".industry").show();
                        }
                    }
                    var curl = location.href;
                    $(".ewb-oper-module").removeClass("ewb-active");
                    $("a.caurl").each(function() {
                        var aurl = $(this).attr("href")
                        if (curl.indexOf(aurl) > -1) {
                            $(this).parent().addClass("ewb-active"); //个人主页的左侧当前导航
                        }
                    })
                });
                // 用户信息
                data.userInfo.loginId = $.cookie('userName');
                $useInfo.html(M.render(userInfoTmpl, data.userInfo));
                //init:base info,only personHome.html
                if ($(".ewb-operation").data("p") == "home") {
                    var cname = $('#ewb-name').text();
                    $('#ewb-cname').val(cname);
                    $("#registContacts").val(data.userInfo.contacts);
                    $("#mobile").val(data.userInfo.mobile);
                    $("#phone").val(data.userInfo.phone); //固定电话
                    $("#qq").val(data.userInfo.QQ);
                    $("#addr").val(data.userInfo.contactsAddress);
					
					$("#birthday").val(data.userInfo.birthday);
					$("#nativeplace").val(data.userInfo.nativeplace);
					$("#address").val(data.userInfo.address);
					$("#school").val(data.userInfo.school);
					$("#major").val(data.userInfo.major);
					$("#education").val(data.userInfo.education);
					$("#degree").val(data.userInfo.degree);
					$("#graduationtime").val(data.userInfo.graduationtime);
					$("#hiredate").val(data.userInfo.hiredate);
					$("#learnexperience").val(data.userInfo.learnexperience);
					$("#workexperience").val(data.userInfo.workexperience);
					
					$("#zjemail").val(data.userInfo.email);
					$("#zjbirthday").val(data.userInfo.birthday);
					$("#zjage").val(data.userInfo.age);
					$("#zjschool").val(data.userInfo.school);
					$("#zjmajor").val(data.userInfo.major);
					$("#zjeducation").val(data.userInfo.education);
					$("#zjdegree").val(data.userInfo.degree);
					$("#techtitle").val(data.userInfo.techtitle);
					$("#techlevel").val(data.userInfo.techlevel);
					$("#workunit").val(data.userInfo.workunit);
					$("#resume").val(data.userInfo.resume);
					$("#yjcgryjl").val(data.userInfo.yjcgryjl);
					
                    if (epointuser.type == "企业注册") {
						$("#cycxmk").attr("style","display:default");
                        $("#tyshxyCode").val(data.userInfo.tyshxyCode);
                        $("#zzjgCode").val(data.userInfo.zzjgCode);
                        $("#gszcCode").val(data.userInfo.gszcNO);
                        $("#swdjCode").val(data.userInfo.swdjNO);
                        $("#unitName").val(data.userInfo.unitName);
                        $("#fddbrName").val(data.userInfo.fddbrName);
                        $("#fddbrID").val(data.userInfo.fddbrID);
                        $("#registAddress").val(data.userInfo.registAddress);
                        if (data.userInfo.registCapital) {
                            var i = data.userInfo.registCapital.indexOf("万");
                            var v = data.userInfo.registCapital.substr(0, i);
                            var d = data.userInfo.registCapital.substr(i + 1, data.userInfo.registCapital.length - i);
                            $("#registCapital").val(v);
                            $("#zbdw").val(d);
                        }
                        $("#gqzsno").val(data.userInfo.gqzsno);
                        $("#fzdate").val(data.userInfo.fzDate);
                        if(data.userInfo.qyType&&data.userInfo.qyType!=undefined)
                        {
                            $("#qyType").val(data.userInfo.qyType);
                        }
                        /* if (data.userInfo.qyType != 1) {
                            $(".selectb").parent().siblings('.tabrdo.gy').hide(); //非工业隐藏规模
                        }
                        if (data.userInfo.qyType != 2) {
                            $(".selectb").parent().siblings('.tabrdo.fwy').hide(); //非服务业隐藏规模
                        } else {
                            if (data.userInfo.fwyScare == 2) {
                                $(".selectb").parent().siblings('.tabrdo.fwy').find("#rdof2").prop("checked", true);
                            }

                        } */
						if(data.userInfo.qyType == 1){
							$("#fwysection").parent().hide();
							$("#fwyScale").parent().hide();
						}else if(data.userInfo.qyType == 2){
							$("#fwysection").parent().hide();
							$("#scaresection").parent().hide();
						}else if(data.userInfo.qyType == ""||data.userInfo.qyType == "undefined"){
							$("#fwysection").parent().hide();
							$("#fwyScale").parent().hide();
							$("#scaresection").parent().show();
						}else{
							$("#fwysection").parent().hide();
							$("#scaresection").parent().hide();
							$("#fwyScale").parent().hide();
						}
                        if (data.userInfo.isyxqngjgxjsqy == "1") {
                            $("#rdogxy").prop("checked", true);
                        } else {
                            $("#rdogxn").prop("checked", true);
                        }
						if($("input[name='isyxqngjgxjsqy']:checked").val()=="0"){
							$("#gqzsno").parent().hide();
							$("#fzdate").parent().hide();
						}
                        epointuser.scale = data.userInfo.scale; //企业规模
                    } else if(epointuser.type == "个人注册"||epointuser.type == "人才注册"){
						$("#cycxmk").attr("style","display:none");
                        if (data.userInfo.sex == "男") {
                            $("#rdoman").prop("checked", true);
                        } else {
                            $("#rdowomen").prop("checked", true);
                        }
                        $("#userxm").val(data.userInfo.name);
                        $("#usersfz").val(data.userInfo.personalID);
                    }else{
						if (data.userInfo.sex == "男") {
                            $("#zjrdoman").prop("checked", true);
                        } else {
                            $("#zjrdowomen").prop("checked", true);
                        }
						$("#zjuserxm").val(data.userInfo.name);
                        $("#zjusersfz").val(data.userInfo.personalID);
					}
                    //认证附件：
                    $.ajax({
                        url: Util.AjaxRUrl + 'getUnitAttachInfo',
                        type: "post",
                        dataType: "json",
                        data: {
                            param: '{rowGuid:"' + the_userid + '"}'
                        },
                        success: function(data) {
                            if (data.wordList) {
                                var fext = false;
                                var furl = Util.FileRUrl;
                                for (var i = 0; i < data.wordList.length; i++) {
                                    var $file = $(".ftag[data-tag=" + data.wordList[i].cliengTag + "]");
                                    if ($file.length > 0) {
                                        fext = true;
                                        $file.html('<span class="fileopr filedelete" data-role="' + data.wordList[i].rowGuid + '">删除</span>|<span class="fileopr filedetail" data-url="' + furl + data.wordList[i].url + '">查看</span>');
                                    }
                                    $file.parent().prev().find("a").css({ top: "-32px" });;
                                }
                                if (fext) {
                                    //delete file
                                    $(".filedelete").click(function(event) {
                                        var $this = $(this);
                                        var fguid = $this.data("role");
                                        var rz = $('#state').text();
                                        if (rz.indexOf("通过") > -1&&$("#confirm").css("display")=="none") {
                                            alert("认证通过后不能删除证件");
                                        } else {
                                            //delete ajax
                                            $.ajax({
                                                url: Util.AjaxRUrl + 'deleteAttachFile',
                                                type: "post",
                                                dataType: "json",
                                                data: {
                                                    param: '{attachGuid:"' + fguid + '"}'
                                                },
                                                success: function(data) {
                                                    if (data.returnInfo) {
                                                        if (data.returnInfo.isok == 1) {
                                                            //success:remove
                                                            alert("已删除");
                                                            $this.parents(".ewb-tr").find("a.webuploader-container").css({ top: "0px" });;
                                                            $this.parent().html(""); //delete ,$this miss
                                                        } else {
                                                            alert("删除失败");
                                                        }
                                                    }
                                                },
                                                error: function(XMLHttpRequest, txtStatus, errorThrown) {
                                                    //console.log(XMLHttpRequest);
                                                }
                                            });
                                        }
                                    });
                                    //look file
                                    $(".filedetail").on('click', function() {
                                        var $this = $(this);
                                        var furl = $this.data("url");
                                        window.open(furl, "_blank");
                                    });
                                }
                            }
                        },
                        error: function(XMLHttpRequest, txtStatus, errorThrown) {
                            //console.log(XMLHttpRequest);
                        }
                    });
                }
            }
        }
    }

    function renderStateData(data) {
        if (data) {
            if (data.returnInfo) {
                var stateName;
                stateName = data.returnInfo.status;
                var result = "认证异常";
                if (stateName != "") {
                    result = stateName;
                }
                $('#state').text(result);
                if (result.indexOf("通过") > -1 && $(".ewb-operation").data("p") == "home") {
                    $(".ewb-confirm").hide(); //pass hide
					$("#personinfoedit").show();
                }
            }
        }
    }
})(jQuery);
